package com.witmore.neutron.ai.web.apiv2.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jk.common.response.ResponseResult;
import com.witmore.neutron.ai.db.entity.AgentDO;
import com.witmore.neutron.ai.db.entity.AgentPublishConfigDO;
import com.witmore.neutron.ai.db.entity.ModelConfigDO;
import com.witmore.neutron.ai.db.mapper.AgentPublishConfigMapper;
import com.witmore.neutron.ai.web.annotation.RequiresPermission;
import com.witmore.neutron.ai.web.apiv2.controller.vo.RobotDetailResult;
import com.witmore.neutron.ai.web.controller.vo.*;
import com.witmore.neutron.ai.web.service.RobotService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * V2 机器人相关接口
 * Author: 豆子高
 * Date: 2025/5/15 15:03
 */
@Slf4j
@RestController
@RequestMapping("/v2/robot")
public class RobotControllerV2 {

    @Value("${witmore.neutron-ai.domain}")
    private String WITMORE_NEUTRON_AI_DOMAIN;

    @Autowired
    private RobotService robotService;
    @Autowired
    private AgentPublishConfigMapper agentPublishConfigMapper;

    /**
     * 机器人列表
     */
    @RequiresPermission("robot:list")
    @GetMapping("/list")
    public ResponseResult<List<AgentResult>> dashboard() {
        return ResponseResult.success(robotService.list(new RobotQueryParam()));
    }

    /**
     * 机器人详情
     */
    @RequiresPermission("robot:detail")
    @GetMapping("/detail/{agentCode}")
    public ResponseResult<RobotDetailResult> robotDetail(@PathVariable("agentCode") String agentCode) {
        RobotDetailResult detailResult = new RobotDetailResult();
        AgentDO agent = robotService.getAgentByCode(agentCode);
        if (agent == null) {
            return ResponseResult.fail(1001, "Robot not found");
        }
        detailResult.setAgent(agent);

        // 发布信息
        List<AgentPublishConfigDO> agentPublishConfigDOS = agentPublishConfigMapper.selectList(new LambdaQueryWrapper<AgentPublishConfigDO>()
                .eq(AgentPublishConfigDO::getAgentCode, agent.getAgentCode())
        );
        // 发布地址组装
        agentPublishConfigDOS.forEach(agentPublishConfigDO -> {
            agentPublishConfigDO.setAgentShowTitle(StringUtils.isEmpty(agentPublishConfigDO.getAgentShowTitle())
                    ? agent.getAgentName() : agentPublishConfigDO.getAgentShowTitle());
            String publishUrl = agentPublishConfigDO.getPublishUrl();
            if (publishUrl != null && !publishUrl.isEmpty()) {
                agentPublishConfigDO.setPublishUrl(WITMORE_NEUTRON_AI_DOMAIN + agentPublishConfigDO.getPublishUrl());
            }
        });
        detailResult.setAgentPublishConfigList(agentPublishConfigDOS);
        return ResponseResult.success(detailResult);
    }

    /**
     * 创建机器人
     */
    @RequiresPermission("robot:create")
    @ResponseBody
    @PostMapping("/create")
    public ResponseResult<Void> create(@RequestBody AgentCreateParam createParam) {
        return robotService.createRobot(createParam);
    }

    /**
     * 获取模型列表
     */
    @RequiresPermission("robot:getModels")
    @ResponseBody
    @GetMapping("/getModels")
    public ResponseResult<List<ModelConfigDO>> getModels() {
        return robotService.getModels();
    }

    /**
     * 更新机器人-详情页保存提交
     */
    @RequiresPermission("robot:update")
    @ResponseBody
    @PostMapping("/update")
    public ResponseResult<Void> update(@Valid @RequestBody RobotUpdateParam param) {
        log.warn("update param:{}", param);
        return robotService.update(param);
    }

    /**
     * 更新机器人名称
     */
    @RequiresPermission("robot:updateName")
    @ResponseBody
    @PostMapping("/updateName")
    public ResponseResult<Void> updateName(@Valid @RequestBody RobotUpdateParam param) {
        return robotService.updateName(param);
    }

    /**
     * 更新机器人发布状态
     */
    @RequiresPermission("robot:updatePublishStatus")
    @ResponseBody
    @PostMapping("/updatePublishStatus")
    public ResponseResult<Void> updatePublishStatus(@Valid @RequestBody AgentPublishConfigParam param) {
        return robotService.updatePublishStatus(param);
    }

    /**
     * 更新机器人发布标题
     */
    @RequiresPermission("robot:updateAgentShowTitle")
    @ResponseBody
    @PostMapping("/updateAgentShowTitle")
    public ResponseResult<Void> updateAgentShowTitle(@Valid @RequestBody AgentPublishConfigParam param) {
        return robotService.updateAgentShowTitle(param);
    }

    /**
     * 删除机器人
     */
    @RequiresPermission("robot:delete")
    @ResponseBody
    @PostMapping("/delete")
    public ResponseResult<Void> delete(@RequestParam("agentId") Integer agentId) {
        return robotService.deleteRobot(agentId);
    }

    /**
     * 锁定机器人
     */
    @RequiresPermission("robot:lock")
    @ResponseBody
    @PostMapping("/lock")
    public ResponseResult<Void> lock(@RequestParam("agentId") Integer agentId) {
        return robotService.lock(agentId);
    }

    /**
     * 解锁机器人
     */
    @RequiresPermission("robot:unlock")
    @ResponseBody
    @PostMapping("/unlock")
    public ResponseResult<Void> unLock(@RequestParam("agentId") Integer agentId) {
        return robotService.unlock(agentId);
    }

    /**
     * 复制机器人
     */
    @RequiresPermission("robot:copy")
    @ResponseBody
    @PostMapping("/copy")
    public ResponseResult<Void> copy(@RequestParam("agentId") Integer agentId) {
        return robotService.copy(agentId);
    }
}
